//
//  XMAddFriendView.m
//  XMJJ_zml
//
//  Created by apple on 2025/5/12.
//

#import "XMAddFriendView.h"
#import "XMAddFriendCell.h"

@interface XMAddFriendView ()<UITableViewDelegate, UITableViewDataSource, UITextFieldDelegate>

@property (nonatomic, strong) UITextField *searchTextField;
@property (nonatomic, strong) UITableView *tbl;
@property (nonatomic, strong) UIView *headView;

@property (nonatomic, strong) NSArray *friendModelArr;

@property (nonatomic, assign) NSInteger selectIndex;
@property (nonatomic, assign) BOOL isSearch;

@end

@implementation XMAddFriendView

- (instancetype)initWithFrame:(CGRect)frame {
    self = [super initWithFrame:frame];
    if (self) {
        [self creatUI];
    }
    return self;
}

- (void)creatUI {
    [self addSubview:self.navView];
    self.navTitleLabel.text = @"添加朋友";
    self.navView.backgroundColor = kColorHEX(0xF6F6F6);
    self.backgroundColor = kColorHEX(0xF0F0F0);
    
    [self addSubview:self.searchTextField];
    [self addSubview:self.tbl];
}

- (void)itemSelectAction:(UIButton *)button {
    if (self.delegate && [self.delegate respondsToSelector:@selector(addFriendFromThirdParty:)]) {
        [self.delegate addFriendFromThirdParty:button.tag - 2000];
    }
}

- (void)setContentWithReFriendModelArr:(NSArray *)reFriendModelArr
                              isSearch:(BOOL)isSearch {
    _isSearch = isSearch;
    _headView.hidden = isSearch;
    _tbl.tableHeaderView = isSearch ? nil : self.headView;
    _friendModelArr = reFriendModelArr;
    [_tbl reloadData];
    [self endEditing:YES];
}

- (void)attentionSuccess {
    XMFriendModel *friendModel = self.friendModelArr[_selectIndex];
    if (friendModel.status.length > 0 && [friendModel.status isEqualToString:@"1"]) {
        friendModel.status = @"0";
    }
    else {
        friendModel.status = @"1";
    }
    [_tbl reloadData];
}

#pragma mark - TblDelegate
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return _friendModelArr.count;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    return KScaleWidth(77.0);
}

- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section {
    return _isSearch ? 0 : KScaleWidth(25.0);
}

- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section {
    UIView *headerView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, self.width, KScaleWidth(25.0))];
    headerView.backgroundColor = kColorHEX(0xffffff);
    UILabel *titleLabel = [[UILabel alloc] initWithFrame:CGRectMake(KScaleWidth(16.0), 0, headerView.width - KScaleWidth(16.0), headerView.height) font:KFontSize(15.0) textColor:kColorHEX(0x666666)];
    titleLabel.text = @"你可能感兴趣的人";
    [headerView addSubview:titleLabel];
    
    return headerView;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    XMAddFriendCell *cell = [tableView dequeueReusableCellWithIdentifier:@"XMAddFriendCellName" forIndexPath:indexPath];
    XMFriendModel *friendModel = _friendModelArr[indexPath.row];
    [cell setContentWithFriendModel:_friendModelArr[indexPath.row]];
    
    XMWeakSelf;
    cell.attentionBlock = ^{
        if (weakSelf.delegate && [weakSelf.delegate respondsToSelector:@selector(friendAttentionWithUserId:status:)]) {
            weakSelf.selectIndex = indexPath.row;
            [weakSelf.delegate friendAttentionWithUserId:friendModel.userId status:friendModel.status];
        }
    };
    return cell;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
}

- (void)scrollViewDidScroll:(UIScrollView *)scrollView {
    [self endEditing:YES];
}

- (BOOL)textFieldShouldReturn:(UITextField *)textField {
    if (self.delegate && [self.delegate respondsToSelector:@selector(searchFriendWithSearchKey:)]) {
        [self.delegate searchFriendWithSearchKey:self.searchTextField.text];
    }
    return YES;
}

#pragma mark - Init
- (UITextField *)searchTextField {
    if (!_searchTextField) {
        _searchTextField = [[UITextField alloc] initWithFrame:CGRectMake(KScaleWidth(16.0), kNavagationBarH + KScaleWidth(12.0), self.width - KScaleWidth(16.0)*2, KScaleWidth(42.0))];
        _searchTextField.backgroundColor = kColorHEX(0xE2E2E2);
        _searchTextField.font = KFontSize(15.0);
        _searchTextField.placeholder = @"搜索用户昵称/ID号";
        _searchTextField.returnKeyType = UIReturnKeySearch;
        _searchTextField.delegate = self;
        [_searchTextField setCornerRadius:_searchTextField.height/2];
        
        UIView *leftView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, KScaleWidth(30.0), _searchTextField.height)];
        UIImageView *searchIcon = [[UIImageView alloc] initWithFrame:CGRectMake((leftView.width - KScaleWidth(14.0))/2, 0, KScaleWidth(14.0), leftView.height)];
        searchIcon.contentMode = UIViewContentModeScaleAspectFit;
        [leftView addSubview:searchIcon];
        _searchTextField.leftView = leftView;
        _searchTextField.leftViewMode = UITextFieldViewModeAlways;
    }
    return _searchTextField;
}

- (UITableView *)tbl {
    if (!_tbl) {
        _tbl = [[UITableView alloc] initWithFrame:CGRectMake(0, kNavagationBarH + KScaleWidth(66.0), self.width, self.height - kNavagationBarH - KScaleWidth(66.0)) style:UITableViewStylePlain];
        _tbl.delegate = self;
        _tbl.dataSource = self;
        if (@available(iOS 15.0, *)) {
            _tbl.sectionHeaderTopPadding = 0;
        } else {
            _tbl.contentInsetAdjustmentBehavior = UIScrollViewContentInsetAdjustmentNever;
            // Fallback on earlier versions
        }
        _tbl.tableHeaderView = self.headView;
        _tbl.separatorInset = UIEdgeInsetsMake(0, KScaleWidth(72.0), 0, 1);
        _tbl.separatorColor = kColorHEX(0xF1F1F1);
        [_tbl registerClass:[XMAddFriendCell class] forCellReuseIdentifier:@"XMAddFriendCellName"];
    }
    return _tbl;
}

- (UIView *)headView {
    if (!_headView) {
        _headView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, self.width, KScaleWidth(113.0))];
        _headView.backgroundColor = kColorHEX(0xffffff);
        
        UILabel *titleLabel = [[UILabel alloc] initWithFrame:CGRectMake(KScaleWidth(16.0), KScaleWidth(11.0), self.width - KScaleWidth(16.0)*2, KScaleWidth(31.0)) font:KFontSize(15.0) textColor:kColorHEX(0x666666)];
        titleLabel.text = @"发现好友";
        [_headView addSubview:titleLabel];
        
        NSArray *titleArr = @[@"通讯录好友", @"微信好友", @"扫一扫"];
        NSArray *itemIconArr = @[@"mine_friend_addressbook_icon", @"mine_friend_weichat_icon", @"mine_friend_scan_icon"];
        NSArray *subTitleArr = @[@"找通讯录好友", @"邀请微信好友", @"扫描名片朋友"];
        CGFloat width = (self.width - KScaleWidth(109.0)*3 - KScaleWidth(16.0)*2)/2;
        for (int i = 0; i < 3; i ++) {
            UIButton *itemBtn = [UIButton buttonWithType:UIButtonTypeCustom];
            itemBtn.frame = CGRectMake(KScaleWidth(16.0) + (KScaleWidth(109.0) + width)*i, KScaleWidth(45.0), KScaleWidth(109.0), KScaleWidth(52.0));
            itemBtn.tag = 2000 + i;
            itemBtn.backgroundColor = kColorHEX(0xF8F8F8);
            [itemBtn setCornerRadius:KScaleWidth(6.0)];
            [itemBtn addTarget:self action:@selector(itemSelectAction:) forControlEvents:UIControlEventTouchUpInside];
            [_headView addSubview:itemBtn];
            
            UIImageView *itemIcon = [[UIImageView alloc] initWithFrame:CGRectMake(KScaleWidth(9.0), KScaleWidth(6.0), KScaleWidth(20.0), KScaleWidth(30.0))];
            itemIcon.contentMode = UIViewContentModeScaleAspectFit;
            itemIcon.image = ImageNamed(itemIconArr[i]);
            [itemBtn addSubview:itemIcon];
            
            UILabel *titleLabel = [[UILabel alloc] initWithFrame:CGRectMake(KScaleWidth(33.0), KScaleWidth(9.0), itemBtn.width - KScaleWidth(33.0), KScaleWidth(18.0)) font:KFontSize(13.0) textColor:kColorHEX(0x666666)];
            titleLabel.text = titleArr[i];
            [itemBtn addSubview:titleLabel];
            titleLabel.userInteractionEnabled = NO;
            
            UILabel *subTitleLabel = [[UILabel alloc] initWithFrame:CGRectMake(titleLabel.x, titleLabel.maxY, titleLabel.width, KScaleWidth(14.0)) font:KFontSize(10.0) textColor:kColorHEX(0x999999)];
            subTitleLabel.text = subTitleArr[i];
            [itemBtn addSubview:subTitleLabel];
            subTitleLabel.userInteractionEnabled = NO;
        }
    }
    return _headView;
}

/*
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect {
    // Drawing code
}
*/

@end
